﻿
namespace SharpDepend.Datatypes
{
    public struct BitVector8
    {
        private byte mValue;

        public const byte MASK0 = 1;
        public const byte MASK1 = 1 << 1;
        public const byte MASK2 = 1 << 2;
        public const byte MASK3 = 1 << 3;
        public const byte MASK4 = 1 << 4;
        public const byte MASK5 = 1 << 5;
        public const byte MASK6 = 1 << 6;
        public const byte MASK7 = 1 << 7;

        public BitVector8(bool allVallue)
        {
            if (allVallue)
            {
                mValue = byte.MaxValue;
            }
            else
            {
                mValue = 0;
            }
        }

        public bool HasAnyValue { get { return mValue > 0; } }

        public bool IsAllTrue { get { return mValue == 255; } }

        public bool Value0
        {
            get { return GetValue(MASK0); }
            set { SetValue(value, MASK0); }
        }

        public bool Value1
        {
            get { return GetValue(MASK1); }
            set { SetValue(value, MASK1); }
        }

        public bool Value2
        {
            get { return GetValue(MASK2); }
            set { SetValue(value, MASK2); }
        }

        public bool Value3
        {
            get { return GetValue(MASK3); }
            set { SetValue(value, MASK3); }
        }

        public bool Value4
        {
            get { return GetValue(MASK4); }
            set { SetValue(value, MASK4); }
        }

        public bool Value5
        {
            get { return GetValue(MASK5); }
            set { SetValue(value, MASK5); }
        }

        public bool Value6
        {
            get { return GetValue(MASK6); }
            set { SetValue(value, MASK6); }
        }

        public bool Value7
        {
            get { return GetValue(MASK7); }
            set { SetValue(value, MASK7); }
        }

        private bool GetValue(byte mask)
        {
            return (mValue & mask) > 0;
        }

        private void SetValue(bool value, byte mask)
        {
            if (value)
            {
                mValue = (byte)(mValue | mask);
            }
            else
            {
                mValue = (byte)(mValue - mask);
            }
        }
    }
}
